import Vue from 'vue'
import axios from 'axios'
import store from '../store'
import { VueAxios } from './axios'
import {Modal, notification} from 'ant-design-vue'
import { ACCESS_TOKEN } from "../store/mutation-types";
import {loginRouterPath} from "../config/router-constant-table";
import router from '../config/router-config'
import {appApiBaseUrl}  from '../config/constant.config'


// 创建 axios 实例
const service = axios.create({
  baseURL: appApiBaseUrl, // api base_url
  timeout: 30000 // 请求超时时间
});
// 返回结果不处理的，适用于第三方api
const noResInterceptors = axios.create({
  baseURL: appApiBaseUrl, // api base_url
  timeout: 30000 // 请求超时时间
});

// 请求错误处理
const err = (error) => {

  if (error.response) {
    let data = error.response.data;
    switch (error.response.status) {
      case 403:
        notification.error({ message: '系统提示', description: '拒绝访问',duration: 4});
        break;
      case 500:
        notification.error({ message: '系统提示', description: '服务器错误，请联系管理员',duration: 4});
        break;
      case 404:
          notification.error({ message: '系统提示', description:'很抱歉，资源未找到!',duration: 4});
        break;
      case 504:
        notification.error({ message: '系统提示', description: '网络超时'});
        break;
      default:
        notification.error({
          message: '系统提示',
          description: data.message||'网络错误',
          duration: 4
        });
        break
    }
  }else{
    notification.error({ message: '系统提示', description: error.toString()});
  }
  return Promise.reject(error);
};

const requestInterceptor = (config) =>{
  const token = Vue.ls.get(ACCESS_TOKEN);
  if (token) {
    config.headers['X-Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  config.headers['X-Requested-With'] = 'XMLHttpRequest'; // 标示是ajax 请求
  if(config.method=='get'){
    config.params = {
      _t: new Date().getTime()/1000,
      ...config.params
    }
  }
  return config
}

// 请求拦截，增加请求头X-Access-Token, X-Requested-With, get 方式增加_t 时间戳，起到不缓存结果作用
service.interceptors.request.use(requestInterceptor,err);

// 请求拦截，增加请求头X-Access-Token, X-Requested-With, get 方式增加_t 时间戳，起到不缓存结果作用
noResInterceptors.interceptors.request.use(requestInterceptor, err);

// 回应拦截
service.interceptors.response.use((response) => {
  // 文件格式不在验证
    if(response.config.responseType == 'blob'){
      return response;
    }
    if(response.config.responseType=='text'){
      return response.data;
    }
    if((response.config.responseType == 'json' || response.headers['content-type'].indexOf("application/json")>-1) && !response.data.success){
      if(response.data.code == 401){
        notification.error({ message: '系统提示', description:'登录失效,请重新登录',duration: 4});
        store.commit('CLEAR_USER');
        router.push({ path: loginRouterPath, query: { redirect: router.currentRoute.fullPath } })
      }else {
        notification.error({ message: '系统提示', description:response.data.message,duration: 4})
      }
    }
    return response.data
  }, err);
// 回应不拦截
noResInterceptors.interceptors.response.use((response)=>response.data, err);

const installer = {
  vm: {},
  install (Vue) {
    Vue.use(VueAxios, service)
  }
}

export {
  installer as VueAxios,
  service as axios,
  noResInterceptors
}
